//销售方案·捆绑销售
(function(){
	var db = require("../db/index");
	var config = require("../_include/config");
	var db_table_name = config.dbname.marketBind;
	var db_id_prefix = config.dbIdPrefix.marketBind;
	var helper = require("../_include/helper");
	var GoodsCon = require("../goods/_con");

	//构造函数
	var _con = function (self,args){
		var goods_ids ;
		if (!args.goods) {
			throwCusError("参数错误")
		}else if((goods_ids = Object.keys(args))===0){
			throwCusError("捆绑销售的商品列表不可为空");
		}
		//检测每个商品是否存在
		goods_ids.forEach(function (goods_id) {
			if (!GoodsCon.get(goods_id)) {
				throwCusError("找不到指定商品")
			}
		});
		//商品列表，格式为：{goods_id:num}
		self.goods = args.goods;
		//套装价格
		var price = +args.price;
		//!=NaN&&>0
		if (!price) {
			throwCusError("指定价格不合理");
		}
		self.price = args.price;
		//限购
		self.limit = args.limit||0;
		//库存量
		self.num = args.num||0;
		//优惠时间区域，为空则不限制，不在时间区域内不可购买
		self.begin_time = args.begin_time;
		self.end_time = args.end_time;
		//所属活动，一个噱头，为了能在活动的优惠列表中显示这个优惠项目
		self.activity = args.activity;

		db.insert(db_table_name,self);
		console.log("创建一个 捆绑销售 方案：",self._id);
	};
	//PS:模块的暴露放在require skill前面，因为二者互相依赖
	module.exports = _con;
	//技能
	_con.prototype = require("./skill");

	//静态方法
	_con.get = function(Id){
		var result = db.find_by_id(db_table_name,Id);
		if(result){
			result.__proto__ = _con.prototype;
		}
		return result;
	};
}());
